Public Class frmViewer
    Private App As WikiApp = WikiApp.GetInstance

    Private Sub ShowPage(ByVal PageType As WikiPage.PageType)
        Try
            wbViewer.DocumentText = App.GetPageHTML(PageType)
        Catch ex As Exception
            MsgBox("Problem: " + ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub
    Private Sub ShowPage(ByVal Title As String)
        Try
            wbViewer.DocumentText = App.GetPageHTML(Title)
        Catch ex As Exception
            MsgBox("Problem: " + ex.Message, MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub Navigate(ByVal Address As String)
        If Address.StartsWith("http://") Then
            wbViewer.Navigate(Address)
        ElseIf Address.StartsWith("wiki://") Then
            ShowPage(Address.Substring(7).Replace("/", ""))
        Else
            ShowPage(Address.Replace("/", ""))
            Address = "wiki://" + Address
        End If
        AddHistoryLink(Address)
        txtAddress.Text = Address
    End Sub

    Private Sub HandleClick(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
        e.ReturnValue = False
        Dim ClickedElement As HtmlElement = wbViewer.Document.ActiveElement
        If Not ClickedElement.TagName.ToLower.Equals("a") Then Exit Sub

        Dim Address As String = ClickedElement.GetAttribute("href").Replace("%20", " ")
        Navigate(Address)
    End Sub

    Private Sub frmViewer_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ShowPage(WikiPage.PageType.StartPage)
        AddHandler wbViewer.Document.Click, AddressOf HandleClick
    End Sub

    Private Sub cmdGotoAddress_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGotoAddress.Click
        Navigate(txtAddress.Text)
    End Sub

    Private Sub cmdGoToStartPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGoToStartPage.Click
        ShowPage(WikiPage.PageType.StartPage)
    End Sub

    Private Sub cmdGoToIndexPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGoToIndexPage.Click
        ShowPage(WikiPage.PageType.IndexPage)
    End Sub

    Private Sub AddHistoryLink(ByVal link As String)
        With lbHistory.Items
            If .Contains(link) Then .Remove(link)
            .Insert(0, link)
        End With
    End Sub

    Private Sub lbHistory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbHistory.SelectedIndexChanged
        If lbHistory.SelectedItem Is Nothing Then Exit Sub
        Navigate(lbHistory.SelectedItem.ToString)
    End Sub

    Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEdit.Click
        If Not txtAddress.Text.StartsWith("wiki://") Then Exit Sub
        Dim pagetitle As String = txtAddress.Text.Substring(7).Replace("/", "")
        Dim frm As New frmEditor(pagetitle, App.Store)
        frm.Show()
    End Sub

    Private Sub cmdRefreshPage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefreshPage.Click
        Navigate(txtAddress.Text)
    End Sub

    Private Sub cmdGoBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGoBack.Click
        If lbHistory.Items.Count > 1 Then
            Navigate(lbHistory.Items(1).ToString)
        End If
    End Sub

    Private Sub cmdPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPrint.Click
        wbViewer.ShowPrintDialog()
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        wbViewer.ShowSaveAsDialog()
    End Sub
End Class
